<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="支付订单日期" prop="orgReqDate" label-width="100px">
        <el-date-picker
          v-model="queryParams.orgReqDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="支付订单流水号" prop="orgHfSeqId" label-width="120px">
        <el-input
          v-model="queryParams.orgHfSeqId"
          placeholder="请输入支付订单流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="收款商户号" prop="huifuId" label-width="100px">
        <el-input
          v-model="queryParams.huifuId"
          placeholder="请输入收款商户号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="收款商户名称" prop="merName" label-width="100px">
        <el-input
          v-model="queryParams.merName"
          placeholder="请输入收款商户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
     
      <el-form-item label="分账接收方编号" prop="divHuifuId" label-width="120px">
        <el-input
          v-model="queryParams.divHuifuId"
          placeholder="请输入分账接收方编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="请求日期" prop="reqDate">
        <el-date-picker
          v-model="queryParams.reqDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="请求流水号" prop="reqSeqId" label-width="100px">
        <el-input
          v-model="queryParams.reqSeqId"
          placeholder="请输入请求流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
       
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['pay:delay-trans-order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="支付日期" align="center" prop="orgReqDate" :formatter="dateFormatter2" width="140px" />
      <el-table-column label="支付流水号" align="center" prop="orgHfSeqId" width="140px" />
      <el-table-column label="收款商户号" align="center" prop="huifuId" width="140px" />
      <el-table-column label="收款商户名称" align="center" prop="merName" width="100px" />
      <el-table-column label="交易总金额" align="center" prop="transAmt" width="140px" :formatter="fenToYuanFormat" />
      <el-table-column label="分账比例" align="center" prop="rebateRate" :formatter="percent" />
      <el-table-column label="分账金额" align="center" prop="divAmt" :formatter="fenToYuanFormat" />
      <el-table-column label="分账接收方编号" align="center" prop="divHuifuId" width="100px" />
      <!-- <el-table-column label="请求日期" align="center" prop="reqDate" /> -->
      <!-- <el-table-column label="请求流水号" align="center" prop="reqSeqId" /> -->
      <el-table-column label="结果" align="center" prop="transState" width="80px" />
      <!-- <el-table-column label="返回状态码" align="center" prop="respCode" /> -->
      <el-table-column label="返回信息" align="center" prop="respDesc" width="80px" />
      <!-- <el-table-column label="请求参数" align="center" prop="reqParam" />
      <el-table-column label="请求结果" align="center" prop="reqResponse" /> -->
      <el-table-column label="重试次数" align="center" prop="reqCount" />
      <el-table-column
        label="最近请求时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="220px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="retry(scope.row.id)"
            v-hasPermi="['pay:delay-trans-order:update']"
          >
            重试
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pay:delay-trans-order:update']"
          >
            详细信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DelayTransOrderForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter,dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { DelayTransOrderApi, DelayTransOrderVO } from '@/api/pay/delaytransorder'
import DelayTransOrderForm from './DelayTransOrderForm.vue'
import { fenToYuanFormat, percent } from '@/utils/formatter'

/** 汇付延迟结算支付订单分账记录	 列表 */
defineOptions({ name: 'DelayTransOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DelayTransOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orgReqDate: [],
  orgHfSeqId: undefined,
  huifuId: undefined,
  merName: undefined,
  transAmt: undefined,
  rebateRate: undefined,
  divAmt: undefined,
  divHuifuId: undefined,
  reqDate: [],
  reqSeqId: undefined,
  transState: undefined,
  respCode: undefined,
  respDesc: undefined,
  reqParam: undefined,
  reqResponse: undefined,
  reqCount: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DelayTransOrderApi.getDelayTransOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DelayTransOrderApi.deleteDelayTransOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DelayTransOrderApi.exportDelayTransOrder(queryParams)
    download.excel(data, '汇付延迟结算支付订单分账记录	.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 重试按钮操作 */
const retry = async (id: number) => {
  try {
    // 重试的二次确认
    await message.confirm("确认重试分账请求？")
    // 发起重试请求
    await DelayTransOrderApi.retryDelayTransOrder(id)
    message.success("重试成功")
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>